﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace FlowPipe
{
    public partial class FlowControl : UserControl
    {
        public FlowControl()
        {
            InitializeComponent();
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            SetStyle(ControlStyles.Selectable, true);
            Timer myTimer = new Timer();
            myTimer.Interval = 500;
            myTimer.Tick += myTimer_Tick;
            myTimer.Enabled = true;
        }

        private void myTimer_Tick(object sender, EventArgs e)
        {
            this.Invalidate();
        }

        private Graphics g;
        private int width;
        private int height;

        private bool isActive;
        public bool IsActive
        {
            get { return isActive; }
            set
            {
                isActive = value;                
            }
        }

        private Color runColor = Color.FromArgb(128, 128, 255);
        public Color RunColor
        {
            get { return runColor; }
            set
            {
                runColor = value;                
            }
        }

        private Color startColor = Color.FromArgb(73, 73, 73);
        public Color StartColor
        {
            get { return startColor; }
            set
            {
                startColor = value;                
            }
        }

        private Color endColor = Color.FromArgb(247, 247, 247);
        public Color EndColor
        {
            get { return endColor; }
            set
            {
                endColor = value;
            }
        }

        private int length = 10;
        public int Length
        {
            get { return length; }
            set
            {
                length = value;
            }
        }

        private double extent = 0.75;
        public double Extent
        {
            get { return extent; }
            set
            {
                extent = value;
            }
        }

        private int space = 5;
        public int Space
        {
            get { return space; }
            set
            {
                space = value;
            }
        }

        private int speed = 5;
        public int Speed
        {
            get { return speed; }
            set
            {
                speed = value;
            }
        }
        //定义水平流动还是垂直流动
        private bool isVertical = false;
        public bool IsVertical
        {
            get { return isVertical; }
            set
            {
                isVertical = value;
            }
        }
        //流动状态1从左到右,2从上到下,3从右到左,4从下到上
        private int flowNum = 1;
        public int FlowNum
        {
            get { return flowNum; }
            set
            {
                flowNum = value;
            }
        }

        int X;
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            g = e.Graphics;
            g.SmoothingMode=SmoothingMode.AntiAlias;
            g.SmoothingMode = SmoothingMode.HighQuality;
            width = this.Width;
            height = this.Height;
            //绘制渐变色的外框
            if (isVertical == false) //水平流动
            {
                if (height % 2 == 0)
                {
                    //上半部分
                    LinearGradientBrush brush;
                    RectangleF rect = new RectangleF(0, 0, width, (float)height / 2);
                    brush = new LinearGradientBrush(rect, startColor, endColor, LinearGradientMode.Vertical);
                    g.FillRectangle(brush, rect);
                    //下半部分
                    RectangleF rect2 = new RectangleF(0, (float)((height / 2)-0.5), width, (float)((height / 2) + 0.5));
                    brush = new LinearGradientBrush(rect2, endColor, startColor, LinearGradientMode.Vertical);
                    g.FillRectangle(brush, rect2);
                }
                else
                {
                    //上半部分
                    LinearGradientBrush brush;
                    RectangleF rect = new RectangleF(0, 0, width, height / 2);
                    brush = new LinearGradientBrush(rect, startColor, endColor, LinearGradientMode.Vertical);
                    g.FillRectangle(brush, rect);
                    //下半部分
                    RectangleF rect2 = new RectangleF(0, (float)((height / 2) - 0.5), width, (float)((height / 2) + 0.5));
                    brush = new LinearGradientBrush(rect2, endColor, startColor, LinearGradientMode.Vertical);
                    g.FillRectangle(brush, rect2);
                }
                    
            }
            else //垂直流动
            {
                if (width % 2 == 0)
                {
                    //左半部分
                    LinearGradientBrush brush;
                    RectangleF rect = new RectangleF(0, 0, (float)width / 2, height);
                    brush = new LinearGradientBrush(rect, startColor, endColor, LinearGradientMode.Horizontal);
                    g.FillRectangle(brush, rect);
                    //右半部分
                    RectangleF rect2 = new RectangleF((float)((width / 2) - 0.5), 0, (float)((width / 2) + 0.5), height);
                    brush = new LinearGradientBrush(rect2, endColor, startColor, LinearGradientMode.Horizontal);
                    g.FillRectangle(brush, rect2);
                }
                else
                {
                    //左半部分
                    LinearGradientBrush brush;
                    RectangleF rect = new RectangleF(0, 0, (float)width / 2, height);
                    brush = new LinearGradientBrush(rect, startColor, endColor, LinearGradientMode.Horizontal);
                    g.FillRectangle(brush, rect);
                    //右半部分
                    RectangleF rect2 = new RectangleF((float)((width / 2) - 0.5), 0, (float)((width / 2) + 0.5), height);
                    brush = new LinearGradientBrush(rect2, endColor, startColor, LinearGradientMode.Horizontal);
                    g.FillRectangle(brush, rect2);
                }
            }

            //绘制流动的线条
            SolidBrush sb;
            switch (flowNum)
            {
                case 1:
                    if (isActive)
                    {
                        sb = new SolidBrush(runColor);
                        if(X> ((length + space) / speed))
                        {
                            X=0;
                        }
                        for (int i = 0; i < width / (length + space); i++)
                        {
                            int recheight = Convert.ToInt32(((1 - extent) / 2) * height);
                            g.FillRectangle(sb, i * (length + space)+speed*X, recheight, length, (int)(height * extent));
                        }
                        X++;
                    }
                    break;
                case 2:
                    if (isActive)
                    {
                        sb = new SolidBrush(runColor);
                        if (X > ((length + space) / speed))
                        {
                            X = 0;
                        }
                        for (int i = 0; i < width / (length + space); i++)
                        {
                            int recheight = Convert.ToInt32(((1 - extent) / 2) * height);
                            g.FillRectangle(sb, (width-length)-(length + space)*i - speed * X, recheight, length, (int)(height * extent));
                        }
                        X++;
                    }
                    break;
                case 3:
                    if (isActive)
                    {
                        sb = new SolidBrush(runColor);
                        if (X > ((length + space) / speed))
                        {
                            X = 0;
                        }
                        for (int i = 0; i < height / (length + space); i++)
                        {
                            int recwidth = Convert.ToInt32(((1 - extent) / 2) * width);
                            g.FillRectangle(sb, recwidth, i * (length + space)  + speed * X, (int)(width * extent), length );
                        }
                        X++;
                    }
                    break;
                case 4:
                    if (isActive)
                    {
                        sb = new SolidBrush(runColor);
                        if (X > ((length + space) / speed))
                        {
                            X = 0;
                        }
                        for (int i = 0; i < height / (length + space); i++)
                        {
                            int recwidth = Convert.ToInt32(((1 - extent) / 2) * width);
                            g.FillRectangle(sb, recwidth, height-length- i * (length + space) - speed * X, (int)(width * extent), length);
                        }
                        X++;
                    }
                    break;
                default:
                    sb = new SolidBrush(runColor);
                    break;
            
            }
            
            
        }
    }
}
